// @flow
import React from 'react'
import { Route } from 'react-router'
import MenuSidebar from 'containers/MenuSidebarContainer'
import MenuSettingPage from './MenuSettingPage'
import MenuAddPage from './MenuAddPage'
import MenuEditPage from './MenuEditPage'
import UserSettingPage from './UserSettingPage'
import UserAddPage from './UserAddPage'
import UserEditPage from './UserEditPage'
import RoleSettingPage from './RoleSettingPage'
import OfficeSettingPage from './OfficeSettingPage'
import LogSettingPage from './LogSettingPage'
import SmsSettingPage from './SmsSettingPage'
import SmsAddPage from './SmsAddPage'
import SmsEditPage from './SmsEditPage'
import MailSettingPage from './MailSettingPage'
import MailAddPage from './MailAddPage'
import MailEditPage from './MailEditPage'
import AMQAddPage from './AMQAddPage'
import AMQEditPage from './AMQEditPage'
import AMQSettingPage from './AMQSettingPage'
import DictSettingPage from './DictSettingPage'
import DictAddPage from './DictAddPage'
import DictEditPage from './DictEditPage'
import RoleAddPage from './RoleAddPage'
import RoleEditPage from './RoleEditPage'
import RegionSettingPage from './RegionSettingPage'
import { connect } from 'react-redux'
import Iframe from 'components/Iframe'

type Props = {
  match: Object,
  history: Object,
  userMenu: Array<Object>
}

type State = {
  menu: Object,
  routes: Array<Object>
}
class SystemManager extends React.Component<Props, State> {
  constructor (props: Props) {
    super(props)
    this.state = {
      menu: {},
      routes: [
        <Route key={'user-add'} path={`${this.props.match.url}/user-add`} component={UserAddPage} />,
        <Route key={'user-edit'} path={`${this.props.match.url}/user-edit`} component={UserEditPage} />,
        <Route key={'menu-add'} path={`${this.props.match.url}/menu-add`} component={MenuAddPage} />,
        <Route key={'menu-edit'} path={`${this.props.match.url}/menu-edit`} component={MenuEditPage} />,
        <Route key={'sms-add'} path={`${this.props.match.url}/sms-add`} component={SmsAddPage} />,
        <Route key={'sms-edit'} path={`${this.props.match.url}/sms-edit`} component={SmsEditPage} />,
        <Route key={'mail-add'} path={`${this.props.match.url}/mail-add`} component={MailAddPage} />,
        <Route key={'mail-edit'} path={`${this.props.match.url}/mail-edit`} component={MailEditPage} />,
        <Route key={'amq-add'} path={`${this.props.match.url}/amq-add`} component={AMQAddPage} />,
        <Route key={'amq-edit'} path={`${this.props.match.url}/amq-edit`} component={AMQEditPage} />,
        <Route key={'dict-add'} path={`${this.props.match.url}/dict-add`} component={DictAddPage} />,
        <Route key={'dict-edit'} path={`${this.props.match.url}/dict-edit`} component={DictEditPage} />,
        <Route key={'role-add'} path={`${this.props.match.url}/role-add`} component={RoleAddPage} />,
        <Route key={'role-edit'} path={`${this.props.match.url}/role-edit`} component={RoleEditPage} />
      ]
    }
  }

  componentWillMount () {
    this.setMenu()
  }
  setMenu () {
    const mainHref = '/system'
    const menuData = this.props.userMenu[0].children.find(item => item.href === mainHref)
    if (!menuData) { return }
    const url = this.props.match.url
    // 系统设置菜单,需要预先定义好左侧菜单地址, 并且配套定义好对应组件供查询
    const srcArr = [`${url}`, `${url}/smsSetting`, `${url}/mailSetting`, `${url}/amqSetting`,
      `${url}/dictSetting`, `${url}/logSetting`, `${url}/userSetting`, `${url}/roleSetting`,
      `${url}/officeSetting`, `${url}/regionSetting`]
    const componentArr = [MenuSettingPage, SmsSettingPage, MailSettingPage, AMQSettingPage, DictSettingPage,
      LogSettingPage, UserSettingPage, RoleSettingPage, OfficeSettingPage, RegionSettingPage]

    let menu = {}
    if (!menuData) return
    //  添加二级菜单
    menuData.children.forEach(item => {
      menu[item.name] = []
      item.children.forEach(children => {
        const { href, name } = children
        const componentIndex = srcArr.indexOf(href)
        if (componentIndex > -1) {
          const exact = href === `${url}` ? 'exact' : ''
          menu[item.name].push([name, href, componentArr[componentIndex], exact])
        }
      })
    })
    // 寻找并添加Iframe
    // const demoMenu = ['百度', '/analysis/https://www.baidu.com', Iframe]
    // menu['任务分析'].push(demoMenu)
    menuData.children.forEach(firstMenu => {
      firstMenu.children.forEach(secMenu => {
        if (secMenu.target === 'Iframe') {
          const { href, name } = secMenu
          const IframeMenu = [ name, `${mainHref}/${href}`, Iframe ]
          menu[firstMenu.name].push(IframeMenu)
        }
      })
    })
    this.setState({ menu })
    // 获取地址,默认选中第一个
    const firstSlectItemKey = Object.keys(menu)
    let firstSlectItemSrc
    for (let i = 0, length = firstSlectItemKey.length; i < length; i++) {
      if (menu[firstSlectItemKey[i]].length) {
        firstSlectItemSrc = menu[firstSlectItemKey[i]][0][1]
        break
      }
    }
    this.props.history.replace(firstSlectItemSrc)
  }
  render () {
    return (
      <MenuSidebar menu={this.state.menu} routes={this.state.routes} style={{ background: '#f5f5f5' }} />
    )
  }
}
const mapStateToProps = state => {
  return {
    userMenu: state.menu.userMenu
  }
}
const mapDispatchToProps = {
}
export default connect(mapStateToProps, mapDispatchToProps)(SystemManager)
